package com.jdy.hotel.ui.register;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.PagerAdapter;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.jdy.hotel.R;
import com.jdy.hotel.check.CheckHelper;
import com.jdy.hotel.net.ResponseData;
import com.jdy.hotel.support.BaseActivity;
import com.jdy.hotel.support.BaseViewModelFactory;
import com.jdy.hotel.support.view.BaseViewItem;
import com.jdy.hotel.support.view.ControlViewPager;
import com.jdy.hotel.support.view.IView;
import com.jdy.hotel.ui.MainActivity;
import com.jdy.hotel.utils.DateUtil;

import java.util.Calendar;


public class RegisterActivity extends BaseActivity {

    private TextView mTextView, userBirthText;
    private View accountView, userView;
    private TimePickerView mStartDatePickerView;
    private ControlViewPager viewPager;

    @Override
    protected int getLayoutSource() {
        return R.layout.activity_register;
    }

    @Override
    protected void createView(Bundle savedInstanceState) {
        accountView = View.inflate(this, R.layout.view_account, null);
        userView = View.inflate(this, R.layout.view_user, null);
        mTextView = findViewById(R.id.register_view_title);
        loadingProgressBar = findViewById(R.id.register_load_progress);
        userBirthText = accountView.findViewById(R.id.register_account_birth);
        viewPager = findViewById(R.id.register_view_pager);
        //初始化ViewPager
        initViewPager(viewPager, new BaseViewItem(accountView, R.string.register_user_information), new BaseViewItem(userView, R.string.register_user_account));
    }

    @Override
    protected void bindModel() {
        RegisterViewModel registerViewModel = ViewModelProviders.of(this, new BaseViewModelFactory()).get(RegisterViewModel.class);

        CheckHelper.of(registerViewModel).edit("accountName", getString(R.string.register_user_name), accountView.findViewById(R.id.register_account_name)).notNull()
                .radio("userSex", getString(R.string.register_user_sex), accountView.findViewById(R.id.register_account_sex_man))
                .text("userBirth", getString(R.string.register_user_birth), userBirthText)
                .edit("userPhone", getString(R.string.register_user_phone), accountView.findViewById(R.id.register_account_phone)).notNull().phone()
                .edit("userEmail", getString(R.string.register_user_email), accountView.findViewById(R.id.register_account_email)).notNull().email()
                .edit("userAddress", getString(R.string.register_user_address), accountView.findViewById(R.id.register_account_address))
                .editTake("userName", getString(R.string.prompt_email), userView.findViewById(R.id.register_user_name))
                .editTake("Password", getString(R.string.prompt_password), userView.findViewById(R.id.register_user_password)).notNull()
                .equal(getString(R.string.prompt_confirm_password), userView.findViewById(R.id.register_user_confirm_password));

        //本地界面数据校验结果
        setFormObserver(registerViewModel.getFormData());
        setRequestObserver(registerViewModel.getRespondData());
        findViewById(R.id.register_action_upload).setOnClickListener(v -> {
            loadingProgressBar.setVisibility(View.VISIBLE);
            registerViewModel.register();
        });
    }

    @Override
    protected void onRespond(ResponseData response) {
        setResult(Activity.RESULT_OK);
        startActivity(new Intent(RegisterActivity.this, MainActivity.class));
        finish();
    }

    @Override
    protected void onFailure(ResponseData response) {
        showMessage(response.getMessage());
    }

    @Override
    protected void onError(View view, String message) {
        PagerAdapter adapter = viewPager.getAdapter();
        if (adapter == null) return;
        viewPager.setCurrentItem(adapter.getItemPosition(view));
    }

    protected void setListeners() {
        findViewById(R.id.register_action_back).setOnClickListener(v -> this.finish());
        userBirthText.setOnClickListener(v -> mStartDatePickerView.show());
    }

    @Override
    protected void bindData(Bundle savedInstanceState) {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        //设置最小日期和最大日期
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(DateUtil.convert("1970-01-01"));//设置为2006年4月28日
        Calendar endDate = Calendar.getInstance();//最大日期是今天
        //时间选择器
        mStartDatePickerView = new TimePickerBuilder(RegisterActivity.this, (date, v) -> {//选中事件回调
            // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
            userBirthText.setText(DateUtil.dateFormat(date));
        }).setDecorView(accountView.findViewById(R.id.account_message_root))//必须是RelativeLayout，不设置setDecorView的话，底部虚拟导航栏会显示在弹出的选择器区域
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("", "", "", "", "", "")
                .isCenterLabel(false)//是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setTitleText("开始日期")//标题文字
                .setTitleSize(20)//标题文字大小
                .setTitleColor(getResources().getColor(R.color.pickerview_title_text_color))//标题文字颜色
                .setCancelText("取消")//取消按钮文字
                .setCancelColor(getResources().getColor(R.color.pickerview_cancel_text_color))//取消按钮文字颜色
                .setSubmitText("确定")//确认按钮文字
                .setSubmitColor(getResources().getColor(R.color.pickerview_submit_text_color))//确定按钮文字颜色
                .setContentTextSize(20)//滚轮文字大小
                .setTextColorCenter(getResources().getColor(R.color.pickerview_center_text_color))//设置选中文本的颜色值
                .setLineSpacingMultiplier(1.8f)//行间距
                .setDividerColor(getResources().getColor(R.color.pickerview_divider_color))//设置分割线的颜色
                .setRangDate(startDate, endDate)//设置最小和最大日期
                .setDate(selectedDate)//设置选中的日期
                .build();
    }

    @Override
    protected void onPageChange(IView view) {
        mTextView.setText(view.getTitle());
    }

}
